package com.ibeetl.cms.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.cms.entity.Enterprise;
import com.ibeetl.cms.entity.Sincerity;
import com.ibeetl.cms.service.EnterpriseService;
import com.ibeetl.cms.service.EssentialInformationService;
import com.ibeetl.cms.service.SincerityService;
import com.ibeetl.cms.util.RoleConstant;
import com.ibeetl.cms.web.query.SincerityQuery;
import com.ibeetl.cms.web.query2.SincerityEnterpriseQuery;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 诚信管理 接口
 */
@Controller
public class SincerityController {

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/cms/sincerity";

    @Autowired
    CorePlatformService platformService;

    @Autowired
    private SincerityService sincerityService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private EssentialInformationService essentialInformationService;

    @Autowired
    FileService fileService;
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @Function("sincerity")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/cms/sincerity/index.html");
        view.addObject("search", SincerityQuery.class.getName());
        return view;
    }

    @GetMapping(MODEL + "/Enterpriseindex.do")
    @Function("sincerity.company")
    @ResponseBody
    public ModelAndView Enterpriseindex() {
        ModelAndView view = new ModelAndView("/cms/sincerity/enterpriseindex.html");
        view.addObject("search", SincerityEnterpriseQuery.class.getName());
        return view;
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("cms.sincerity.edit")
    @ResponseBody
    public ModelAndView edit(Integer id) {
        ModelAndView view = new ModelAndView("/cms/sincerity/edit.html");
        Sincerity sincerity = sincerityService.queryById(id);
        view.addObject("sincerity", sincerity);
        return view;
    }

    @GetMapping(MODEL + "/add.do")
    @Function("sincerity")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/cms/sincerity/add.html");
        return view;
    }

    @GetMapping(MODEL + "/enterpriseadd.do")
    @Function("sincerity.company")
    @ResponseBody
    public ModelAndView enterprise() {
        ModelAndView view = new ModelAndView("/cms/sincerity/enterpriseadd.html");
        List<Enterprise> enterprises = enterpriseService.findAll();
        view.addObject("enterprise", enterprises);
        return view;
    }

    /* ajax json */
    //个人诚信
    @PostMapping(MODEL + "/list.json")
    @Function("sincerity")
    @ResponseBody
    public JsonResult<PageQuery> list(SincerityQuery condtion) {
        condtion.setUserType(""+RoleConstant.PERSON);
        PageQuery page = condtion.getPageQuery();
        sincerityService.queryByCondition(page);
        return JsonResult.success(page);
    }

    //单位诚信
    @PostMapping(MODEL + "/enterpriselist.json")
    @Function("sincerity.company")
    @ResponseBody
    public JsonResult<PageQuery> enterpriselist(SincerityQuery condtion) {
        condtion.setUserType(""+RoleConstant.COMPANY);
        PageQuery page = condtion.getPageQuery();
        sincerityService.enterpriselist(page);
        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/add.json")
    @Function("sincerity")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class) Sincerity sincerity, HttpServletRequest request) {
        String[] genders = request.getParameterValues("standard[]");
        if (genders == null) {
            genders = request.getParameterValues("standard");
        }
        Integer identificationNumber = essentialInformationService.findorgUserId(sincerity.getIdentificationNumber());
        if (identificationNumber == null) {
            return JsonResult.failMessage("没有找到对应的证件号码");
        }
        sincerity.setType(""+RoleConstant.PERSON);
        sincerity.setTypeId(identificationNumber);
        if(null != genders && genders.length > 0) {
            //字符串类型数组转字符串
            String gendersStr = StringUtils.join(genders, ",");
            sincerity.setBusinessType(gendersStr);
            sincerityService.save(sincerity);
            return new JsonResult().success();
        }else{
            return JsonResult.failMessage("业务事项不能为空");
        }
    }

    @PostMapping(MODEL + "/enterpriseadd.json")
    @Function("sincerity.company")
    @ResponseBody
    public JsonResult enterpriseadd(@Validated(ValidateConfig.ADD.class) Sincerity sincerity, HttpServletRequest request) {
        String[] genders = request.getParameterValues("standard[]");
        if (genders == null) {
            genders = request.getParameterValues("standard");
        }
        sincerity.setType(""+RoleConstant.COMPANY);
        Integer organizationCode = enterpriseService.findorgUserId(sincerity.getOrganizationCode());
        if (organizationCode == null) {
            return JsonResult.failMessage("没有找到对应的统一社会信用代码");
        }
        sincerity.setTypeId(enterpriseService.findorgUserId(sincerity.getOrganizationCode()));
        //字符串类型数组转字符串
        String gendersStr = StringUtils.join(genders, ",");
        if (genders != null) {
                sincerity.setBusinessType(gendersStr);
                sincerityService.save(sincerity);
            return new JsonResult().success();
        }else{
            return JsonResult.failMessage("业务事项不能为空");
        }
    }

    //未启用改功能方法 20190109 liujun
    /*@PostMapping(MODEL + "/update.json")
    @Function("cms.sincerity.update")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class) Sincerity sincerity, HttpServletRequest request) {
        String[] genders = request.getParameterValues("standard[]");
        if (genders == null) {
            genders = request.getParameterValues("standard");
        }
        Integer identificationNumber = essentialInformationService.findorgUserId(sincerity.getIdentificationNumber());
        if (identificationNumber == null) {
            return JsonResult.failMessage("没有找到对应的证件号码");
        }
        sincerity.setType("个人");
        sincerity.setTypeId(identificationNumber);
        //字符串类型数组转字符串
        String gendersStr = StringUtils.join(genders, ",");
        sincerity.setBusinessType(gendersStr);
        boolean success = sincerityService.updateTemplate(sincerity);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }*/


    @GetMapping(MODEL + "/view.json")
    @Function("cms.sincerity.query")
    @ResponseBody
    public JsonResult<Sincerity> queryInfo(Integer id) {
        Sincerity sincerity = sincerityService.queryById(id);
        return JsonResult.success(sincerity);
    }

    @PostMapping(MODEL + "/delete.json")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        sincerityService.batchDelSincerity(idList);
        return new JsonResult().success();
    }

    /**
     * 查看个人页面跳转
     *
     * @return
     */
    @GetMapping(MODEL + "/find.do")
    @ResponseBody
    public ModelAndView find(Integer id) {
        ModelAndView view = new ModelAndView("/cms/sincerity/find.html");
        Sincerity sincerity = sincerityService.queryById(id);
        view.addObject("sincerity", sincerity);
        return view;
    }

    /**
     * 查看单位诚信页面跳转
     *
     * @return
     */
    @GetMapping(MODEL + "/findenterprise.do")
    @ResponseBody
    public ModelAndView findenterprise(Integer id) {
        ModelAndView view = new ModelAndView("/cms/sincerity/findenterprise.html");
        Sincerity sincerity = sincerityService.queryById(id);
        view.addObject("sincerity", sincerity);
        return view;
    }


}
